var combo;
var list;
function accountData()
{

    //var combo = document.getElementById("used_accounts_list");
    eval('var data = ' + combo.selectedItem.value);
    return data;
}
function doSave()
{

    var data = accountData();
    //var mDBConn = connectDb();
    var saveBannedStat = mDBConn.createStatement("REPLACE INTO banned_users(service, username, banned_user) VALUES(?1, ?2, ?3)");
    var removeBannedStat = mDBConn.createStatement("DELETE FROM banned_users WHERE service=?1 AND username=?2");
    removeBannedStat.bindUTF8StringParameter(0, data.service);
    removeBannedStat.bindUTF8StringParameter(1, data.username);
    removeBannedStat.execute();
    removeBannedStat.reset();
    //var list = document.getElementById("banned_users_list");
    for(var i = 0; i < list.getRowCount(); i++)
    {
        if(!list.getItemAtIndex(i).hidden)
        {
            saveBannedStat.bindUTF8StringParameter(0, data.service);
            saveBannedStat.bindUTF8StringParameter(1, data.username);
            saveBannedStat.bindUTF8StringParameter(2, list.getItemAtIndex(i).label);
            saveBannedStat.execute();
            saveBannedStat.reset();
        }
    }
	enumAllDocuments(init_LJR_Helper);
}
function doCancel(){
    return true;
}
function onOptionsLoaded()
{
    window.sizeToContent();
    
    combo = document.getElementById("used_accounts_list");
    list = document.getElementById("banned_users_list");
    if(combo.childNodes.length > 0)
    {
        combo.selectedIndex = 1;
        accountSelectionChanged();
    }
}
function accountSelectionChanged()
{
    //var combo = document.getElementById("used_accounts_list");
    var data = accountData();
    document.getElementById("username").textContent = data.username;
    document.getElementById("service").textContent = data.service;
    document.getElementById("banned_users_list").builder.rebuild();

}
function delBanned()
{
    //var list = document.getElementById("banned_users_list");
    list.removeItemAt(list.selectedIndex);
}
function addBanned()
{
	var newUserText = document.getElementById('new_user_text');
	var val = newUserText.value.replace(/^\s*(\S(.*\S)?)\s*$/, '$1');
	if(val == '')
	{
		alert("Username is empty. Enter something and then retry.");
		return;
	}
	if(!/^\w*$/.test(val))
	{
		alert("Username contains incorrect characters. Username can contain letters, digits and '_' only. Please, correct username and try again.");
		return;
	}
	var found = false;
	for(var i = 0; i < list.getRowCount(); i++)
	{
		var item = list.getItemAtIndex(i);
		if(val == item.label)
		{
			found = true;
			if(item.hidden)
			{
				item.hidden = false;
			}
			else
			{
				alert("Such a user is already banned");
			}
		}
	}
	if(!found)
	{
		list.appendItem(val, val);
	}
	newUserText.value = "";
}
